package com.base.common.util;

import org.jboss.netty.buffer.ChannelBuffer;

/**
 * @author yanjiaqiang
 * @date 2011-1-11
 * @version $Id: ChannelBufferUtils.java 7 2011-07-15 04:24:04Z JiaqiangYan@gmail.com $
 */
public class ChannelBufferUtils {

	/**
	 * 获取0-writerIndex的数组。
	 * 
	 * @param buffer
	 * @return
	 */
	public static byte[] getUsedArray(ChannelBuffer buffer) {
		byte[] bs = null;
		if (buffer.writable()) {
			int wIndex = buffer.writerIndex();
			bs = new byte[wIndex];
			buffer.getBytes(0, bs);
		} else {
			bs = buffer.array();
		}
		return bs;
	}

	public static byte[] getReadableBytes(ChannelBuffer buffer) {
		byte[] bs = new byte[buffer.readableBytes()];
		buffer.readBytes(bs);
		return bs;
	}

}
